<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('宠物列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>宠物名：</label>
                                <input type="text" name="name" placeholder="请输入准确的宠物名" onkeyup="handleEnterKey(event)"/>
                            </li>
                            <li>
                                <label>出生日期：</label>
                                <input type="text" class="time-input" placeholder="请选择出生日期" name="birthday" onkeyup="handleEnterKey(event)"/>
                            </li>
                            <li>
                                <label>客户名：</label>
                                <input type="text" name="clientName" placeholder="请输入客户名" onkeyup="handleEnterKey(event)"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:pets:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:pets:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:pets:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:pets:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:pets:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:pets:remove')}]];
        var prefix = ctx + "system/pets";
        // 假设 petTypeDict 是通过 Thymeleaf 注入的宠物品种字典数据
        var petTypeDict = /*[[${@dict.getType('sys_pet_type')}]]*/ [];
        // 将字典数据转换为键值对形式，方便查找
        var petTypeMap = {};
        petTypeDict.forEach(function(item) {
            petTypeMap[item.dictValue] = item.dictLabel;
        });
        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "宠物",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'petId',
                    title: '宠物ID',
                    visible: false
                },
                {
                    field: 'name',
                    title: '宠物名'
                },
                {
                    field: 'type',
                    title: '品种',
                    formatter: function(value) {
                        if (typeof value === 'object') {
                            console.error('type 字段的值是一个对象:', value);
                            return '未知';
                        }
                        const label = petTypeMap[value];
                        if (label) {
                            return label;
                        } else {
                            console.error('未找到对应的品种标签，value:', value);
                            return '未知品种';
                        }
                    }
                },
                {
                    field: 'birthday',
                    title: '出生日期'
                },
                {
                    field: 'clientName',
                    title: '客户名'
                },
                {
                    title: '健康状况',
                    field: 'healthStatus',
                    width: '10',
                    widthUnit: '%',
                    align: "left",
                    formatter: function(value) {
                        if (value == "0") {
                            return '<span class="label label-primary">健康</span>';
                        }
                        else if (value == "1") {
                            return '<span class="label label-warning">生病</span>';
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.petId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.petId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function handleEnterKey(event) {
            // console.log(event.keyCode)
            if (event.keyCode === 13) {
                $.table.search();
            }
        }
    </script>
</body>
</html>